package cn.doitedu.crawler;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2020/8/25
 **/
public class JdCrawler {
    public static void main(String[] args) {

        // 系统中需要提前安装chrome浏览器，并把chrome浏览器的selenium驱动器放置在某路径
        System.setProperty("webdriver.chrome.driver","D:/devapps/chromedriver_win32/chromedriver.exe");

        //创建chrome参数对象
        ChromeOptions options = new ChromeOptions();

        //浏览器后台运行，如果不设置，则会打开浏览器界面
        options.addArguments("headless");

        // 加载chrome浏览器驱动器
        ChromeDriver driver = new ChromeDriver(options);
        // 打开网页
        driver.get("https://list.jd.com/list.html?cat=737,794,798");
        // 获取到打开的网页的源码
        String pageSource = driver.getPageSource();
        // 使用jsoup解析html文档
        Document doc = Jsoup.parse(pageSource);
        // 抽取html中的元素
        Element j_goodsList = doc.getElementById("J_goodsList");
        Elements li = j_goodsList.getElementsByTag("li");
        for (Element element : li) {
            String price = element.getElementsByClass("p-price").get(0).getElementsByTag("i").text();
            String title = element.getElementsByClass("p-name p-name-type-3").get(0).getElementsByTag("a").get(0).getElementsByTag("em").text();
            String comment = element.getElementsByClass("p-commit").get(0).getElementsByTag("a").text();
            System.out.println(title + " --> " +price + " --> " + comment);
        }

        driver.close();
    }
}
